package com.lwbldy.modules.student.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lwbldy.common.tools.R;
import com.lwbldy.common.tools.PageUtils;
import com.lwbldy.common.tools.StringUtils;

import com.lwbldy.modules.student.domain.ChargesItems;
import com.lwbldy.modules.student.domain.ChargesType;
import com.lwbldy.modules.student.domain.Major;
import com.lwbldy.modules.student.service.ChargesItemsService;
import com.lwbldy.modules.student.service.ChargesTypeService;
import com.lwbldy.modules.student.service.MajorService;

import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.ModelMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 * 专业 Controller
 * @author lwb
 * @email lwbldy@163.com
 * @date 2022-06-13 10:33:21
 */
@Controller
@Api(tags = "专业")
@RequestMapping("/student/major")
public class MajorController {

    /**
     * Describe: 基础路径
     * */
    private static String MODULE_PATH = "student/major/";


    @Autowired
    private MajorService majorService;
    @Autowired
    private ChargesItemsService chargesItemsService;
    @Autowired
    private ChargesTypeService chargesTypeService;

    @ApiOperation(value="获取列表视图")
    @GetMapping("/main")
    public String main(){
        return MODULE_PATH + "main";
    }


    @ApiOperation(value = "分页查询专业")
    @PostMapping("/list")
    @ResponseBody
    public R list(
            @RequestParam(value = "majorName",required = false) String majorName,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "limit", defaultValue = "15") Integer limit){
        PageHelper.startPage(page, limit);
        Map<String,Object> primaryMap = new HashMap<>();
        primaryMap.put("majorName",majorName);
        List<Major> brandList = majorService.queryList(primaryMap);
        return PageUtils.restPage(brandList);
    }


    @ApiOperation(value = "获取新增视图")
    @GetMapping("add")
    public String add(ModelMap model){
        addtypesAll(model,null);
        return MODULE_PATH + "add";
    }

    @ApiOperation(value = "保存")
    @PostMapping("save")
    @ResponseBody
    public R save(Major major,String[] charges_name,String[] money,HttpServletRequest request, HttpServletResponse response){
        return majorService.saveMajor(major,charges_name,money);
    }

    @ApiOperation(value = "修改显示")
    @PostMapping("changeShow")
    @ResponseBody
    public R changeShow(int id,int isshow){
        return R.resultRow(majorService.changeShow(id,isshow));
    }

    private void addtypesAll(ModelMap model,Integer schoolId){
        List<ChargesType> listAll = chargesTypeService.queryAll();
        if(schoolId != null){
            for(int i = 0;i<listAll.size();i++){
                ChargesType c = listAll.get(i);
                if(!c.getSchool().equals(schoolId)){
                    listAll.remove(i);
                    i--;
                }
            }
        }

        for(ChargesType s : listAll){
            if(s.getSchool() == 1){
                s.setName("(技师)"+s.getName());
            }else if(s.getSchool() == 2){
                s.setName("(农校)"+s.getName());
            }
        }
        model.put("listAllEntity",listAll);
        String jsonStr = JSONArray.toJSONString(listAll);
        model.put("typesAll",jsonStr);
    }

    @ApiOperation(value = "获取修改视图")
    @GetMapping("edit")
    public String edit(ModelMap model, Integer id){
        Major major = majorService.queryObject(id);
        model.put("major",major);
        model.put("chargesItems",chargesItemsService.queryByMajorId(id));
        addtypesAll(model,major.getSchoolId());
        return MODULE_PATH + "edit";
    }

    @ApiOperation(value = "修改信息")
    @PutMapping("update")
    @ResponseBody
    public R update(Major major,String[] charges_name,String[] money){
        return majorService.update(major,charges_name,money);
    }

    @ApiOperation(value = "根据ID删除")
    @DeleteMapping("remove/{id}")
    @ResponseBody
    public R remove(@PathVariable Integer id){
        int result = majorService.delete(id);
        return R.resultRow(result);
    }

    @DeleteMapping("removeBatch/{ids}")
    @ApiOperation(value="批量删除数据")
    @ResponseBody
    public R removeBatch(@PathVariable String ids){
        int result = majorService.deleteBatch(StringUtils.StringIdsToArr(ids));
        return R.resultRow(result);
    }

    @ApiOperation(value = "分页查询专业")
    @GetMapping("/queryByLevelId")
    @ResponseBody
    public List<Major> queryByLevelId(Integer levelId){
        Map<String,Object> primaryMap = new HashMap<>();
        primaryMap.put("levelId",levelId);
        List<Major> brandList = majorService.queryList(primaryMap);
        return brandList;
    }

}
